package actions;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import db.dao.CourClass;
import db.dao.CourClassDAO;
import db.dao.CourClassId;
import db.dao.Course;
import db.dao.CourseDAO;
import db.dao.Teacher;
import db.dao.TeacherDAO;
/*添加开班课程信息*/
public class AddCourClassAction {
	private String tid1="";	//上课教师编号
	private String cid1="";	//课程编号
	private Byte stunum=0;	//课堂学生容量
	private String place="";	//上课地点
	
	public AddCourClassAction() {
	}

	public String getTid1() {
		return tid1;
	}

	public void setTid1(String tid1) {
		this.tid1 = tid1;
	}

	public String getCid1() {
		return cid1;
	}

	public void setCid1(String cid1) {
		this.cid1 = cid1;
	}

	public Byte getStunum() {
		return stunum;
	}

	public void setStunum(Byte stunum) {
		this.stunum = stunum;
	}

	public String getPlace() {
		return place;
	}

	public void setPlace(String place) {
		this.place = place;
	}
	
	public String execute() throws IOException {
		//获取页面信息，用于弹窗提示和清除页面缓存
		HttpServletResponse response = ServletActionContext.getResponse();
		//用于获取结果
		String result="";
		CourClassDAO ccDao = new CourClassDAO();
		CourClass courClass = new CourClass();
		//设置参数信息
		CourClassId courClassId = new CourClassId();
		courClassId.setCourseid(cid1);
		courClassId.setTeaid(tid1);
		courClass.setId(courClassId);
		courClass.setStunumbers(stunum);
		courClass.setPlace(place);
		//查找上课课程信息
		CourseDAO cDao = new CourseDAO();
		Course course = cDao.findById(cid1);
		courClass.setCourse(course);
		//查找上课教师信息
		TeacherDAO tDao = new TeacherDAO();
		Teacher teacher = tDao.findById(tid1);
		courClass.setTeacher(teacher);
		//保存
		result = ccDao.save(courClass);
		if (result.equals("success")) {
			//成功弹窗提示
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("<script>alert('添加成功!')</script>");
            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}else {
			//失败弹窗提示
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("<script>alert('添加失败!')</script>");
            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}
	}
}
